﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormApplication.FormBase;
using WindowsFormsView;
using WindowsFormsPresenter.InventoryItemCategory;
using WindowsFormsModel.BusinessObjects;
using Utils;
using Infragistics.Win.UltraWinTree;
using Infragistics.Win.UltraWinEditors;

namespace WindowsFormApplication.FormCategoryTree
{
    public partial class frmMaterialCategoryList : frmBaseTreeList, IInventoryItemCategoriesView
    {
        private InventoryItemCategoriesPresenter _inventoryItemCategoriesPresenter;
        private bool IsMaterial = true;

        public IList<InventoryItemCategoryModel> InventoryItemCategories
        {
            set
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds = value.AsDataSet(TableName);

                    ds.Relations.Add(RelationName, ds.Tables[TableName].Columns[TablePrimaryKey], ds.Tables[TableName].Columns[TableParentKey], false);
                    treeMain.DataSource = ds.Tables[TableName];

                    UltraTreeNode node = new UltraTreeNode();
                    node = treeMain.TopNode;

                    if (treeMain.Nodes.Count > 0)
                    {
                        while (node != null)
                        {
                            node.DataKey = node.Cells[TablePrimaryKey].Value;
                            node.Key = node.DataKey.ToString();
                            node = node.NextVisibleNode;
                        }
                        treeMain.Nodes[0].Selected = true;
                    }

                    treeMain.ColumnSettings.ColumnSets[0].Columns["InventoryCategoryID"].Visible = false;
                    treeMain.ColumnSettings.ColumnSets[0].Columns["ParentID"].Visible = false;
                    treeMain.ColumnSettings.ColumnSets[0].Columns["IsParent"].Visible = false;
                    treeMain.ColumnSettings.ColumnSets[0].Columns["IsMaterial"].Visible = false;
                    treeMain.ColumnSettings.ColumnSets[0].Columns["RestaurantID"].Visible = false;

                    treeMain.ColumnSettings.ColumnSets[0].Columns["InventoryCategoryCode"].Text = "Mã loại nguyên liệu";
                    treeMain.ColumnSettings.ColumnSets[0].Columns["InventoryCategoryName"].Text = "Tên loại nguyên liệu";
                    treeMain.ColumnSettings.ColumnSets[0].Columns["Description"].Text = "Mô tả";
                    treeMain.ColumnSettings.ColumnSets[0].Columns["IsActive"].Text = "Được sử dụng";

                    UltraCheckEditor chkIsActive = new UltraCheckEditor();
                    chkIsActive.CheckAlign = ContentAlignment.MiddleCenter;
                    treeMain.ColumnSettings.ColumnSets[0].Columns["IsActive"].EditorComponent = chkIsActive;
                }
                catch (Exception) { }
            }
        }

        protected override void InitData()
        {
            try { _inventoryItemCategoriesPresenter.Display(IsMaterial); }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi từ hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        protected override void DeleteRow()
        {
            try
            {
                if (treeMain.ActiveNode != null)
                {
                    string inventoryCategoryId = treeMain.ActiveNode.Cells[TablePrimaryKey].Value.ToString();
                    new InventoryItemCategoryPresenter(null).Delete(inventoryCategoryId);
                    ShowMessageBox("Xóa dữ liệu hoàn thành !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ShowMessageBox("Bạn chưa chọn dòng để xóa !", "Thông báo xóa dữ liệu !", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { ShowMessageBox(ex.Message, "Thông báo lỗi hệ thống !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        public frmMaterialCategoryList()
        {
            InitializeComponent();

            _inventoryItemCategoriesPresenter = new InventoryItemCategoriesPresenter(this);
        }
    }
}
